package com.shop.cereshop.agent.page.user;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.math.BigDecimal;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shop.cereshop.commons.domain.agent.CereAgentUser;
import com.shop.cereshop.commons.domain.agent.CereAgentPrivate;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@ApiModel(value = "CereAgentUserVO", description = "返回前端用户信息")
@Accessors(chain = true)
public class CereAgentUserVO {

  @ApiModelProperty(value = "用户ID")
  private Long agentUserId;

  @ApiModelProperty(value = "角色类型：1-代理商，2-业务员")
  private Integer roleType;

  @ApiModelProperty(value = "账户状态：0-禁用，1-启用")
  private Integer state;

  @ApiModelProperty(value = "是否加入黑名单 1-是 0-否")
  private Integer ifBlack;

  @ApiModelProperty(value = "邀请码")
  private String inviteCode;

  @ApiModelProperty(value = "邀请人ID,顶级代理商或业务员为null")
  private Long invitedUserId;

  @ApiModelProperty(value = "请求token")
  private String token;

  @ApiModelProperty(value = "注册时的ip")
  private String registerIp;

  @ApiModelProperty(value = "最近一次登录时的ip")
  private String lastLoginIp;

  @ApiModelProperty(value = "创建时间")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  private String createTime;

  @ApiModelProperty(value = "更新时间")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  private String updateTime;

  @ApiModelProperty(value = "逻辑删除状态：0=未删除，1=已删除")
  private Integer deleted;

  /**
   * 是否设置支付密码 0-否 1-是
   */
  @ApiModelProperty(value = "是否设置支付密码 0-否 1-是")
  @TableField("is_set_pay_password")
  private Integer isSetPayPassword;

  @ApiModelProperty(value = "是否正式(0-非正式，1-正式)")
  private Integer ifFormal;

  @ApiModelProperty(value = "总消费金额")
  private BigDecimal totalPurchaseAmount;

  @ApiModelProperty(value = "代理商手机号")
  private String phone;

  /**
   * 请求token过期时间
   */
  @ApiModelProperty(value = "请求token过期时间(单位/秒)")
  private Long expire;

  /**
   * 刷新token
   */
  @ApiModelProperty(value = "刷新token")
  private String refreshToken;

  /**
   * 刷新token过期时间
   */
  @ApiModelProperty(value = "刷新token过期时间(单位/秒)")
  private Long refreshExpire;

  @ApiModelProperty(value = "用户最后一次登录的角色类型：1-代理商，2-业务员")
  private Integer lastLoginType;

  @ApiModelProperty(value = "用户角色私有信息id")
  private Long agentPrivateId;

  @ApiModelProperty(value = "用户实名状态：0未认证，1审核中，2已认证，3认证失败")
  private Integer realnameState;

  @ApiModelProperty(value = "用户拥有几种角色，0-都有，1-只有代理商，2-只有业务员，3-都没有")
  private Integer hasRole;

  @ApiModelProperty(value = "用户是否签署合同：0-否，1-是")
  private Integer signState;

  public static CereAgentUserVO fromUserAndPrivate(CereAgentUser cereAgentUser, CereAgentPrivate privateInfo) {
    if (cereAgentUser == null || privateInfo == null) {
      return null;
    }
    CereAgentUserVO vo = new CereAgentUserVO();
    // 来自 CereAgentUser 表
    vo.setAgentUserId(cereAgentUser.getAgentUserId());
    vo.setPhone(cereAgentUser.getPhone());
    vo.setIfBlack(cereAgentUser.getIfBlack());
    vo.setState(cereAgentUser.getState());
    vo.setRegisterIp(cereAgentUser.getRegisterIp());
    vo.setLastLoginIp(cereAgentUser.getLastLoginIp());
    vo.setLastLoginType(cereAgentUser.getLastLoginType());

    // 来自 CereAgentPrivate 表
    vo.setAgentPrivateId(privateInfo.getAgentPrivateId());
    vo.setRoleType(privateInfo.getRoleType());
    vo.setInviteCode(privateInfo.getInviteCode());
    vo.setDeleted(privateInfo.getDeleted());
    vo.setIfFormal(privateInfo.getIfFormal());
    vo.setTotalPurchaseAmount(BigDecimal.valueOf(
        privateInfo.getTotalPurchaseAmount() != null ? privateInfo.getTotalPurchaseAmount() : 0));

    return vo;
  }

  public static CereAgentUserVO fromCereAgentUser(CereAgentUser user) {
    if (user == null) {
      return null;
    }
    CereAgentUserVO agentUser = new CereAgentUserVO();
    agentUser.setAgentUserId(user.getAgentUserId());
    agentUser.setPhone(user.getPhone());
    agentUser.setIfBlack(user.getIfBlack());
    agentUser.setState(user.getState());
    agentUser.setRegisterIp(user.getRegisterIp());
    agentUser.setLastLoginIp(user.getLastLoginIp());
    agentUser.setLastLoginType(user.getLastLoginType());
    return agentUser;
  }

  public static CereAgentUserVO fromCereAgentPrivate(CereAgentPrivate privateInfo) {
    if (privateInfo == null) {
      return null;
    }
    CereAgentUserVO agentUser = new CereAgentUserVO();
    agentUser.setAgentUserId(privateInfo.getAgentUserId())
        .setAgentPrivateId(privateInfo.getAgentPrivateId())
        .setRoleType(privateInfo.getRoleType())
        .setInviteCode(privateInfo.getInviteCode())
        .setDeleted(privateInfo.getDeleted())
        .setIfFormal(privateInfo.getIfFormal())
        .setTotalPurchaseAmount(BigDecimal.valueOf(
            privateInfo.getTotalPurchaseAmount() != null ? privateInfo.getTotalPurchaseAmount() : 0));
    return agentUser;
  }

}
